package test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;

import communication.*;



public class FileSender {

	public static void main(String[] args) {
		try {

			File f = new File (args[0]);
			
			//System.out.println(f.getCanonicalPath());

			BufferedInputStream buffIn = new BufferedInputStream(new FileInputStream(f)); 

			ClientConnection client1 = new ClientConnection(InetAddress.getLocalHost(), 4444);

			BufferedOutputStream buffOut = new BufferedOutputStream (client1.getOutputStream()); 

			DataOutputStream out = new DataOutputStream(client1.getOutputStream());

			out.writeUTF(f.getName());
			
			
			byte [] b = new byte[4096];
			int bytesRead;

			//While there's something left to read, keep sending bytes.
			while ((bytesRead = buffIn.read(b)) != -1) 
			{
				buffOut.write(b, 0, bytesRead);
			}
			buffOut.close();
			buffIn.close();


		} catch (IOException e) {
			System.out.println("Server error.");
			e.printStackTrace();
		} 
	}
}